package se.penselnsvag.bookdb.tech.rpc
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import se.penselnsvag.bookdb.tech.log.LogManager;
	import se.penselnsvag.bookdb.tech.log.LoggerInterface;

	public class DataUpdateProxy implements DataListener
	{
		private var funk:Function;
		private var fault:Function;
		private var log:LoggerInterface;

		public function DataUpdateProxy(dataUpdate:Function, faultUpdate:Function) {
			log = LogManager.getInstance().getLogger(this);
			this.funk = dataUpdate;
			this.fault = faultUpdate;
		}

		public function dataUpdate(updatedData:ResultEvent):void
		{
			if(funk==null) {
				log.warning("dataUpdate with event:"+updatedData+" is ignored!");
				return;
			}
			log.debug("Applying funk:");
			var data:Array = new Array();
			data.push(updatedData);
			funk.apply(null, data);
		}
		
		public function dataFault(updatedData:FaultEvent):void
		{	
			if(fault==null) {
				log.warning("dataFault with event:"+updatedData+"+is ignored!");
				return;
			}
			
			var data:Array = new Array();
			data.push(updatedData);
			fault.apply(this, data);
		}
			
		public function get updateFunction():Function {
			return funk;
		}
		
		public function set updateFunction(funk:Function):void {
			this.funk = funk
		}
		
		public function get faultFunction():Function {
			return fault;
		}
		
		public function set faultFunction(funk:Function):void {
			this.fault = funk
		}
	}
}